from django.db import models

# Create your models here.
from meiduo_mall.utils.basemodel import BaseModel


class GoodsLeibie(BaseModel):
    name = models.CharField(max_length=10, verbose_name='名称')

    parent = models.ForeignKey('self', null=True, blank=True, on_delete=models.CASCADE, verbose_name='父类别')

    class Meta:
        db_table = 'tb_goods_category'
        verbose_name = '商品类别'
        verbose_name_plural = verbose_name

    def __str__(self):
        return self.name


class GoodsPindao(BaseModel):
    group_id = models.IntegerField(verbose_name='组号')

    category = models.ForeignKey(GoodsLeibie, on_delete=models.CASCADE, verbose_name='顶级商品类别')

    url = models.CharField(max_length=50, verbose_name='频道页面链接')

    sequence = models.IntegerField(verbose_name='组内顺序')

    class Meta:
        db_table = 'tb_goods_channel'
        verbose_name = '商品频道'
        verbose_name_plural = verbose_name

    def __str__(self):
        return self.category.name


class Pinpai(BaseModel):
    name = models.CharField(max_length=20, verbose_name='名称')

    logo = models.ImageField(verbose_name='Logo图片')

    first_letter = models.CharField(max_length=1, verbose_name='品牌首字母')

    class Meta:
        db_table = 'tb_pinpai'
        verbose_name = '品牌'
        verbose_name_plural = verbose_name

    def __str__(self):
        return self.name


class Goods(BaseModel):
    name = models.CharField(max_length=50, verbose_name='名称')

    brand = models.ForeignKey(Pinpai, on_delete=models.PROTECT, verbose_name='品牌')

    category1 = models.ForeignKey(GoodsLeibie, on_delete=models.PROTECT, related_name='cat1_goods', verbose_name='一级类别')

    category2 = models.ForeignKey(GoodsLeibie, on_delete=models.PROTECT, related_name='cat2_goods', verbose_name='二级类别')

    category3 = models.ForeignKey(GoodsLeibie, on_delete=models.PROTECT, related_name='cat3_goods', verbose_name='三级类别')

    sales = models.IntegerField(default=0, verbose_name='销量')

    comments = models.IntegerField(default=0, verbose_name='评价数')

    desc_detail = models.CharField(max_length=200, verbose_name='商品描述', default='')
    desc_pack = models.CharField(max_length=200, verbose_name='商品包装', default='')
    desc_service = models.CharField(max_length=200, verbose_name='售后服务', default='')

    class Meta:
        db_table = 'tb_goods'
        verbose_name = '商品'
        verbose_name_plural = verbose_name

    def __str__(self):
        return self.name


class GoodsGuige(BaseModel):
    goods = models.ForeignKey(Goods, on_delete=models.CASCADE, verbose_name='商品')

    name = models.CharField(max_length=20, verbose_name='规格名称')

    class Meta:
        db_table = 'tb_goods_specification'
        verbose_name = '商品规格'
        verbose_name_plural = verbose_name

    def __str__(self):
        return '%s: %s' % (self.goods.name, self.name)


class Guigexuanxiang(BaseModel):
    spec = models.ForeignKey(GoodsGuige, on_delete=models.CASCADE, verbose_name='规格')

    value = models.CharField(max_length=20, verbose_name='选项值')

    class Meta:
        db_table = 'tb_specification_option'
        verbose_name = '规格选项'
        verbose_name_plural = verbose_name

    def __str__(self):
        return '%s - %s' % (self.spec, self.value)


class SKU(BaseModel):
    name = models.CharField(max_length=50, verbose_name='名称')

    caption = models.CharField(max_length=100, verbose_name='副标题')

    goods = models.ForeignKey(Goods, on_delete=models.CASCADE, verbose_name='商品')

    category = models.ForeignKey(GoodsLeibie, on_delete=models.PROTECT, verbose_name='从属类别')

    price = models.DecimalField(max_digits=10, decimal_places=2, verbose_name='单价')

    cost_price = models.DecimalField(max_digits=10, decimal_places=2, verbose_name='进价')

    market_price = models.DecimalField(max_digits=10, decimal_places=2, verbose_name='市场价')

    stock = models.IntegerField(default=0, verbose_name='库存')

    sales = models.IntegerField(default=0, verbose_name='销量')

    comments = models.IntegerField(default=0, verbose_name='评价数')

    is_launched = models.BooleanField(default=True, verbose_name='是否上架销售')

    default_image_url = models.CharField(max_length=200, default='', null=True, blank=True, verbose_name='默认图片')

    class Meta:
        db_table = 'tb_sku'
        verbose_name = '商品SKU'
        verbose_name_plural = verbose_name

    def __str__(self):
        return '%s: %s' % (self.id, self.name)


class SKUImage(BaseModel):
    sku = models.ForeignKey(SKU, on_delete=models.CASCADE, verbose_name='sku')

    image = models.ImageField(verbose_name='图片')

    class Meta:
        db_table = 'tb_sku_image'
        verbose_name = 'SKU图片'
        verbose_name_plural = verbose_name

    def __str__(self):
        return '%s %s' % (self.sku.name, self.id)


class SKUGuige(BaseModel):
    sku = models.ForeignKey(SKU, on_delete=models.CASCADE, verbose_name='sku')

    spec = models.ForeignKey(GoodsGuige, on_delete=models.PROTECT, verbose_name='规格名称')

    option = models.ForeignKey(Guigexuanxiang, on_delete=models.PROTECT, verbose_name='规格值')

    class Meta:
        db_table = 'tb_sku_specification'
        verbose_name = 'SKU规格'
        verbose_name_plural = verbose_name

    def __str__(self):
        return '%s: %s - %s' % (self.sku, self.spec.name, self.option.value)


class GoodsVisitCount(BaseModel):
    """统计分类商品访问量模型类"""
    category = models.ForeignKey(GoodsLeibie,
                                 on_delete=models.CASCADE,
                                 verbose_name='商品分类')
    count = models.IntegerField(verbose_name='访问量', default=0)
    date = models.DateField(auto_now_add=True, verbose_name='统计日期')

    class Meta:
        db_table = 'tb_goods_visit'
        verbose_name = '统计分类商品访问量'
        verbose_name_plural = verbose_name